package com.train.order.facade.web;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.train.common.constants.SentinelResConstants;
import com.train.common.resp.CommonResp;
import com.train.common.utils.helper.UserHelper;
import com.train.order.core.handler.sentinel.SentinelBlockHandler;
import com.train.order.dto.pay.PayOrderTicketDTO;
import com.train.order.req.pay.PayOrderTicketReq;
import com.train.order.service.PayOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("webPayOrderFacade")
@RequiredArgsConstructor
@RequestMapping("pay-order")
public class PayOrderFacade {

    private final PayOrderService payOrderService;

//    private final TestService testService;

    @PostMapping("pay")
    @SentinelResource(value = SentinelResConstants.RSC_NAME_PAY_ORDER,
            blockHandlerClass = SentinelBlockHandler.class,
            blockHandler = SentinelResConstants.HANDLER_PAY_ORDER_BLOCK) //定义降级处理器
    public CommonResp pay(@RequestBody @Validated PayOrderTicketReq req) {
        PayOrderTicketDTO dto = BeanUtil.copyProperties(req, PayOrderTicketDTO.class);
        dto.setMemberId(UserHelper.getUserId());
        payOrderService.pay(dto);
//        testService.test();
        return CommonResp.ok();
    }
}
